package com.lyfei.ydc.service;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.google.gson.Gson;
import com.lyfei.ydc.core.LocalUser;
import com.lyfei.ydc.domain.entity.User;
import com.lyfei.ydc.mapper.UserMapper;
import org.springframework.stereotype.Service;

import java.util.Map;

@Service("userService")
public class UserService extends BaseService<UserMapper, User> {

    public User getUserById(Long id) {
        return this.getById(id);
    }

    public User getByOpenId(String openId) {
        return this.getOne(new LambdaQueryWrapper<User>().eq(User::getOpenid, openId));
    }

    public void updateUserWxInfo(Map<String, Object> wxUser) {
        User user =this.getUserById(LocalUser.getUser().getId());
        user.setNickname(wxUser.get("nickName").toString());
        Gson gson = new Gson();
        user.setWxProfile(gson.toJson(wxUser));
        this.updateById(user);
    }
}
